package com.inxedu.os.common.util.user;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserStudentUtil {
    public static boolean isPasswordAvailable(String password) {
        String partten = "^[_0-9a-zA-Z]{6,}$";
        boolean flag = password.matches(partten) && password.length() >= 6 && password.length() <= 16;
        return flag;
    }
    public Map<String, Object> setJson(boolean success, String message, Object entity) {
        Map<String, Object> json = new HashMap();
        json.put("success", success);
        json.put("message", message);
        json.put("entity", entity);
        return json;
    }

    public static File createExcel(String[] headName, List<List<String>> list, String expName, String dir) throws Exception {

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("sheet1");
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell((short)0);



        int x;
        for(x = 0; x < headName.length; ++x) {
            cell = row.createCell((short)x);
            cell.setCellValue(headName[x]);
        }

        for(x = 0; x < list.size(); ++x) {
            row = sheet.createRow(x + 1);
            List<String> rowString = (List)list.get(x);

            for(int i = 0; i < rowString.size(); ++i) {
                cell = row.createCell((short)i);
                cell.setCellValue((String)rowString.get(i));
            }
        }

        File file = new File(dir + "/" + expName + ".xls");
        if (!(new File(dir)).exists()) {
            (new File(dir)).mkdirs();
        }

        FileOutputStream fos = new FileOutputStream(file);
        workbook.write(fos);
        fos.close();
        return file;
    }


}
